package com.example.customdialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CustomAlertDialog extends Dialog {

    private String mTitle = "";
    private int mTitleId = -1;

    private String mMessage = "";
    private int mMessageId = -1;

    private int mTextColor = -1;

    private int mBtnBgResId = -1;

    private OnClickListener mOnClickListener;


    public CustomAlertDialog(Context context) {
        super(context, R.style.CustomAlertDialog);
    }

    public CustomAlertDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomAlertDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_alert_dialog_layout);
        initView();
    }

    private void initView() {
        TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
        if (-1 != mTitleId) {
            tvTitle.setText(mTitleId);
        } else {
            tvTitle.setText(mTitle);
        }
        TextView tvMessage = (TextView) findViewById(R.id.tvMessage);
        if (-1 != mMessageId) {
            tvMessage.setText(mMessageId);
        } else {
            tvMessage.setText(mMessage);
        }
        if (-1 != mTextColor) {
            tvTitle.setTextColor(mTextColor);
            tvMessage.setTextColor(mTextColor);
        }
        Button btnCancel = (Button) findViewById(R.id.btnCancel);
        Button btnConfirm = (Button) findViewById(R.id.btnConfirm);
        if (-1 != mBtnBgResId) {
            btnCancel.setBackgroundResource(mBtnBgResId);
            btnConfirm.setBackgroundResource(mBtnBgResId);
        }
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomAlertDialog.this.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(CustomAlertDialog.this);
                }
            }
        });
    }


    @Override
    public void create() {
        super.create();
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void hide() {
        super.hide();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }


    public CustomAlertDialog setButtonBackgroundResource(int resId) {
        mBtnBgResId = resId;
        return this;
    }
    public CustomAlertDialog setTextColor(int color) {
        mTextColor = color;
        return this;
    }
    public CustomAlertDialog setTitleText(int resId) {
        mTitleId = resId;
        return this;
    }
    public CustomAlertDialog setTitleText(String title) {
        mTitle = title;
        return this;
    }
    public CustomAlertDialog setMessageText(int resId) {
        mMessageId = resId;
        return this;
    }
    public CustomAlertDialog setMessageText(String message) {
        mMessage = message;
        return this;
    }

    public CustomAlertDialog setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
        return this;
    }

    public interface OnClickListener {
        void onClick(Dialog dialog);
    }
}
